package com.tech.connect.huanzhewan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.ksy.common.image.ImageLoader;
import com.ksy.common.utils.AndroidUtil;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.GridOffsetsItemDecoration;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.activity.CityChooseActivity;
import com.tech.connect.activity.SearchActivity;

import java.util.ArrayList;
import java.util.List;

public class HuanZheWanActivity extends BaseActivity {


    private RecyclerView recycler;
    private List<String> allList = new ArrayList<>();
    private BaseAdapter<String, BaseHolder> adapter;
    private HuanZheWanMainFragment.TitleListener titleListener;
    private int height;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huanzhewan_main);
        getHeadBar().setLeftIcon(R.drawable.icon_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        }).setTitle("换着玩", 0, R.drawable.icon_arrow_down_white).setTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (titleListener != null) {
                    titleListener.onFragmentTitleClick();
                }
            }
        }).setRightIcon(R.drawable.icon_search, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(SearchActivity.class);
            }
        });
        initView();
        initAdapter();
    }


    private void initView() {
        int screenWidth = AndroidUtil.getScreenWidth(activity);
        height = (screenWidth - AndroidUtil.dip2px(30)) / 2;

        TextView tvCity = findViewById(R.id.tvCity);
        findViewById(R.id.vCity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(CityChooseActivity.class, 10001);
            }
        });
        TextView tvCat = findViewById(R.id.tvCat);
        findViewById(R.id.vCat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(HuanZheWanCatChooseActivity.class, 10002);
            }
        });
        TextView tvOptions = findViewById(R.id.tvOptions);
        findViewById(R.id.vOptions).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(HuanZheWanOptionsChooseActivity.class, 10003);
            }
        });

        recycler = findViewById(R.id.recycler);
        recycler.setLayoutManager(new GridLayoutManager(activity, 2) {
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        });
        GridOffsetsItemDecoration itemDecoration = new GridOffsetsItemDecoration(GridOffsetsItemDecoration.GRID_OFFSETS_VERTICAL);
        itemDecoration.setHorizontalItemOffsets(AndroidUtil.dip2px(10));
        itemDecoration.setVerticalItemOffsets(AndroidUtil.dip2px(10));
        recycler.addItemDecoration(itemDecoration);
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_huanzhewan, allList, 6) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ImageView ivImage = helper.getView(R.id.ivImage);
                    ImageLoader.getInstance().load(activity, R.drawable.common_icon_image_placeholder, ivImage, new RequestOptions().override(height));
                }
            };
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent=new Intent(activity,HuanZheWanWuPinViewActivity.class);
                    intent.putExtra("wuPinTitle","苹果6s");
                    jump2Activity(intent);
                }
            });
            recycler.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }

    public void setTitleListener(HuanZheWanMainFragment.TitleListener titleListener) {
        this.titleListener = titleListener;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 10001) {
                //城市切换
            } else if (requestCode == 10002) {
                //宝物切换
            } else if (requestCode == 10003) {
                //条件切换
            } else {

            }
        }
    }
}
